package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.AuthorService;
import com.usian.model.article.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/article-au")
public class AuthorController implements AuthorControllerApi{
    @Autowired
    AuthorService authorService;

    /**
     * 袁浩 2022-6-24
     * 根据用户ID查找作者信息
     * @param id
     * @return
     */
    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return authorService.findById(id);
    }

    /**
     * 袁浩 2022-6-24
     * 创建作者(新增)
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        authorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据作者ID查找作者
     * @param id
     * @return
     */
    @GetMapping("/findByauthId/{id}")
    @Override
    public ApAuthor findByauthId(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }

    /**
     * 根据文章名称查作者姓名
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public ApAuthor findByName(@PathVariable("name") String name) {
        ApAuthor apAuthor = authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        return apAuthor;
    }
}
